cc.Class({
    extends: cc.Component,

    properties: {
        spine: sp.Skeleton,

        skinWings: cc.SpriteFrame,
        skinWings2: cc.SpriteFrame,
        skinDecorate: cc.SpriteFrame,

        skinBody: cc.SpriteFrame,
        skinAmL: cc.SpriteFrame,
        skinAmR: cc.SpriteFrame,
        skinHandR: cc.SpriteFrame,
        skinlothse_back: cc.SpriteFrame,

        skinHairBackL: cc.SpriteFrame,
        skinHairFrontL: cc.SpriteFrame,
        skinHairBackS: cc.SpriteFrame,
        skinHairFrontS: cc.SpriteFrame,
        skinHairShadowS: cc.SpriteFrame,
        skinHairShadowL: cc.SpriteFrame,

        skinFace: cc.SpriteFrame,
    },

    onLoad() {
        cc.debug.setDisplayStats(false);
        // this.loadDressConfig()
        this.dressUpSpine()
    },

    onStart() {
        // this.dressUpSpine()
    },

    dressUpSpine() {
        this.dressUpFace();

        this.dressUp('3_clothse_body',this.skinBody);
        this.dressUp('3_clothse_arm_L',this.skinAmL);
        this.dressUp('3_clothse_arm_R',this.skinAmR);
        this.dressUp('3_clothse_hand_R',this.skinHandR);
        this.dressUp('3_clothse_back',this.skinlothse_back);
 
        this.dressUp('4_decorate',this.skinDecorate);
        this.dressUp('4_wings',this.skinWings);
        this.dressUp('4_wings2',this.skinWings2);

        this.dressUp('1_hair_front',this.skinHairFrontS);
        this.dressUp('1_hair_back',this.skinHairBackS);
        this.dressUp('1_hair_shadow',this.skinHairShadowS);
    },

    dressUpFace() {
        var data = this.spine.skeletonData.getRuntimeData();
        var skin = data.defaultSkin;
        var slot = this.spine.findSlot('2_face')
        const slotIndex = data.findSlotIndex('2_face');      
        var atts = skin.attachments[slotIndex];
        for (const key in atts) {
            if (atts.hasOwnProperty(key)) {
                var att = atts[key];
                var texture = this.skinFace.getTexture()
                if (att instanceof sp.spine.MeshAttachment) {
                    att.region = this.createRegion(texture);
                    att.updateUVs();
                } else if (att instanceof sp.spine.RegionAttachment){
                    att.setRegion(this.createRegion(texture));
                    att.updateOffset();
                }
                // att.updateWorldVertices(slot, true);
            }
        }
    },

    dressUp(slotName,spriteFrame) {
        var slot = this.spine.findSlot(slotName)
        var texture = spriteFrame.getTexture()
        var oldAtt = slot.getAttachment();

        if (oldAtt instanceof sp.spine.MeshAttachment) {
            this.createMeshAttachmentByTexture(slot,texture, oldAtt);
        } else if (oldAtt instanceof sp.spine.RegionAttachment) {
            this.createRegionAttachmentByTexture(slot,texture, oldAtt);
        }
        
    },

    createMeshAttachmentByTexture(slot, texture, oldAtt) { 
        var att = new sp.spine.MeshAttachment(oldAtt.name);
        att.region = this.createRegion(texture);
        att.bones = oldAtt.bones;
        att.color = oldAtt.color;
        att.hullLength = oldAtt.hullLength;
        att.inheritDeform = oldAtt.inheritDeform;
        att.path = oldAtt.path;
        att.regionUVs = oldAtt.regionUVs;
        att.tempColor = oldAtt.tempColor;
        att.triangles = oldAtt.triangles;
        att.vertices = oldAtt.vertices;
        att.worldVertices = oldAtt.worldVertices;
        att.worldVerticesLength = oldAtt.worldVerticesLength;

        att.updateUVs();
        // att.updateWorldVertices(slot, true);

        slot.attachment = att;
    },

    createRegionAttachmentByTexture(slot, texture, oldAtt) {
        var att = new sp.spine.RegionAttachment(oldAtt.name);
        att.region = this.createRegion(texture);
        att.color = oldAtt.color;
        att.path = oldAtt.path;
        att.tempColor = oldAtt.tempColor;
        att.vertices = oldAtt.vertices;
        att.rotation = oldAtt.rotation;
        att.scaleX = oldAtt.scaleX;
        att.scaleY = oldAtt.scaleY;
        att.offset = oldAtt.offset;
        att.width = oldAtt.width;
        att.height = oldAtt.height;
        att.x = oldAtt.x;
        att.y = oldAtt.y;
        att.setRegion(att.region);
        att.updateOffset();
        // att.updateWorldVertices(slot, true);
        
        slot.attachment = att;
    },

    createRegion(texture) {
        var region = new sp.spine.TextureAtlasRegion();
        region.width = texture.width;
        region.height = texture.height;
        region.originalWidth = texture.width;
        region.originalHeight = texture.height;
        region.rotate = false;
        region.u = 0;
        region.v = 0;
        region.u2 = 1;
        region.v2 = 1;

        region.page = new sp.spine.TextureAtlasPage();
        region.page.name = texture.name;
        region.page.width = texture.width;
        region.page.height = texture.height;
        region.page.uWrap = sp.spine.TextureWrap.ClampToEdge;
        region.page.vWrap = sp.spine.TextureWrap.ClampToEdge;

        var texBody = new sp.SkeletonTexture({ width: texture.width, height: texture.height });
        texBody.setRealTexture(texture)
        region.texture = texBody;
    
        return region;
    },

    loadDressConfig() {
        cc.loader.loadResDir("config", cc.JsonAsset,  (err, assets, urls)=> {
            if (err) {
                console.log(config,"加载资源出错------",config)
            } else {
                this.cachConfig(assets)
            }
        });
    },

    cachConfig(assets){
        for (const key in assets) {
            if (assets.hasOwnProperty(key)) {
                const jsonAsset = assets[key];
                if (jsonAsset.name == "dressConf") {
                    this._dressConf = jsonAsset.json
                }
            }
        }
        console.log("dressConf ----",this._dressConf)
        this.prepareDressInfo()
    },

    prepareDressInfo() {
        var spineName = this.spine.skeletonData.name
        if (this._dressConf) {
            this._spineDressInfo = this._dressConf[spineName]
            if (this._spineDressInfo) {
                for (const key in this._spineDressInfo) {
                    const confKey = this._spineDressInfo[key];
                    var dressDir = this._dressConf["resource_map"][confKey][0]
                    var slot_array = this._dressConf["slot_map"][confKey]["slot_array"]
                    this.dressUpOnePlace(dressDir,slot_array)
                }
            } else {
                console.log("spine dressConf not found-------",spineName)
            }
        } else {
            console.log("spine dress 配置信息不存在-------")
        }
    },

    dressUpOnePlace(dressDir,slot_array) {
        for (const key in slot_array) {
            const slotData = slot_array[key];
            var slotName = slotData["name"]
            // var imgArr = slotData["img_array"]
            var img_name = slotData["img_array"][0]["img_name"]
            this.dresUpOneSlot(slotName,img_name,dressDir)
        }
    },

    dresUpOneSlot(slotName,img_name,dressDir) {
        var dirPath = `dressImg/${dressDir}/${img_name}`
        console.log("资源路径------",dirPath)
        cc.loader.loadRes(dirPath,cc.SpriteFrame,(err, spriteFrame)=>{
            if (err) {
                console.log("加载图片出错-------")
            } else {
                this.dressUp(slotName,spriteFrame);
            }
        })
    },
});
